/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Constant;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.PushController;
import com.hboxs.asl.dao.PaymentMethodDao;
import com.hboxs.asl.dao.QuestionDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.PaymentMethod;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.pingpp.Constants;
import com.hboxs.asl.service.FinancialRecordService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.MessageService;
import com.hboxs.asl.service.QuestionService;
import com.pingplusplus.model.Charge;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Service - 提问问题
 */
@Service("questionServiceImpl")
public class QuestionServiceImpl extends BaseServiceImpl<Question, Long> implements QuestionService {

    @Resource(name = "questionDaoImpl")
    private QuestionDao questionDao;
    @Resource(name= "paymentMethodDaoImpl")
    private PaymentMethodDao paymentMethodDao;
    @Resource(name = "financialRecordServiceImpl")
    private FinancialRecordService financialRecordService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "messageServiceImpl")
    private MessageService messageService;

    @Resource(name = "questionDaoImpl")
    public void setBaseDao(QuestionDao questionDao) {
        super.setBaseDao(questionDao);
    }


    @Override
    public Question findBySn(String sn) {
        return questionDao.findBySn(sn);
    }

    @Override
    public void dealQuestion(Charge charge, Question question) {
        PaymentMethod paymentMethod = findPaymentMethod(charge.getChannel());
        question.setPaymentMethod(paymentMethod);
        question.setPaymentMethodName(paymentMethod.getName());
        question.setPaymentStatus(Question.PaymentStatus.paid);
        //用户易问易答支出记录
        financialRecordService.expenditureRecordQuestion(question, null);
        Member teacher = question.getTeacher();
        if (teacher != null) {
            teacher.setBalance(teacher.getBalance().add(question.getPayPrice()));
            //我的收入
            teacher.setEarnings(teacher.getEarnings().add(question.getPayPrice()));
            memberService.update(teacher);
            //老师收入记录
            financialRecordService.incomeRecordQuestion(question, null);
            //TODO 老师发送通知
            String information = question.getMember().getName() + Constant.QUESTION_NOTICE;
//            PushController.noticeTest(teacher, information, question.getId(), Constant.GETUI_QUESTION);
            PushController.notice(teacher, information);
            //TODO 保存消息
            messageService.generateMessage(question.getMember(), teacher, information);
        }
        this.update(question);
    }

    @Override
    public void dealQuestion(Question question) {
        PaymentMethod paymentMethod = findPaymentMethod(Constants.POINT_AMOUNT_PAY);
        question.setPaymentMethod(paymentMethod);
        question.setPaymentMethodName(paymentMethod.getName());
        question.setPaymentStatus(Question.PaymentStatus.paid);
        //用户易问易答支出记录
        financialRecordService.expenditureRecordQuestion(question, Constant.POINT_PAY);
        Member teacher = question.getTeacher();
        if (teacher != null) {
            //老师余额收入
            teacher.setBalance(teacher.getBalance().add(question.getPayPrice()));
            //我的收入
            teacher.setEarnings(teacher.getEarnings().add(question.getPayPrice()));
            memberService.update(teacher);
            //老师收入记录
            financialRecordService.incomeRecordQuestion(question, Constant.POINT_PAY);
            //TODO 老师发送通知
            String information = question.getMember().getName() + Constant.QUESTION_NOTICE;
//            PushController.noticeTest(teacher, information, question.getId(), Constant.GETUI_QUESTION);
            PushController.notice(teacher, information);
            //TODO 保存消息
            messageService.generateMessage(question.getMember(), teacher, information);
        }
        this.update(question);
    }

    @Override
    public Page<Question> findQuestionPage(Pageable pageable, Member member, Question.PaymentStatus paymentStatus, Question.AnswerStatus answerStatus) {
        return questionDao.findQuestionPage(pageable, member, paymentStatus, answerStatus);
    }

    @Override
    public Page<Question> findQuestionPage(Pageable pageable, Member member, Question.PaymentStatus paymentStatus) {
        return questionDao.findQuestionPage(pageable, member, paymentStatus);
    }

    @Override
    public Page<Question> findQuestionByTeacher(Pageable pageable, Member teacher, Question.PaymentStatus paymentStatus, Question.AnswerStatus answerStatus) {
        return questionDao.findQuestionByTeacher(pageable, teacher, paymentStatus, answerStatus);
    }

    @Override
    public Page<Question> findPageGroup(Pageable pageable, Question.PaymentStatus paymentStatus, Question.AnswerStatus answerStatus) {
        return questionDao.findPageGroup(pageable, paymentStatus, answerStatus);
    }

    @Override
    public Page<Question> findQuestionPage(Pageable pageable, Member member) {
        return questionDao.findQuestionPage(pageable, member);
    }

    @Override
    public Page<Question> findPageByPayment(Pageable pageable) {
        return questionDao.findPageByPayment(pageable);
    }

    @Override
    public Page<Question> findPageByAnswer(Pageable pageable) {
        return questionDao.findPageByAnswer(pageable);
    }

    @Override
    public List<Question> findQuestionList() {
        return questionDao.findQuestionList();
    }

    @Override
    public long getQuestionCount(Question.AnswerStatus answerStatus) {
        return questionDao.getQuestionCount(answerStatus);
    }

    @Override
    public long myAnswerCounts(Member teacher) {
        return questionDao.myAnswerCounts(teacher);
    }

    /**
     * 查找对应的支付方式
     */
    private PaymentMethod findPaymentMethod(String channel) {
        List<PaymentMethod> paymentMethods = paymentMethodDao.findList(null, null, null, null);
        HashMap<String, Integer> modeOfPayment = new HashMap<>();
        modeOfPayment.put(Constants.CHANNEL_WX, 0);
        modeOfPayment.put(Constants.CHANNEL_ALIPAY, 1);
        modeOfPayment.put(Constants.CHANNEL_UPACP, 2);
        modeOfPayment.put(Constants.CHANNEL_AMOUNT_PAY, 3);
        modeOfPayment.put(Constants.POINT_AMOUNT_PAY, 5);
        return paymentMethods.get(modeOfPayment.get(channel));
    }


}
